<?php

function getSystemSettings($leagueId) {
	$sql = "SELECT * from settings where isSystem = true and leagueId = $leagueId order by id;";
	
	$result = mysql_query($sql);
	
	$settings = new Settings();
	
	$row = mysql_fetch_array($result);
	$settings->trades = $row['value'];
	
	$row = mysql_fetch_array($result);
	$settings->timeLimit = $row['value'];

	$row = mysql_fetch_array($result);
	$settings->bot = $row['value'];
	
	$row = mysql_fetch_array($result);
	$settings->timeLength = $row['value'];
	
	$row = mysql_fetch_array($result);
	$settings->tradesPageID = $row['value'];
	
	$row = mysql_fetch_array($result);
	$settings->picksPageID = $row['value'];
	
	$row = mysql_fetch_array($result);
	$settings->seasonInProgress = $row['value'];

	$row = mysql_fetch_array($result);
	$settings->submittedTeamsPageID = $row['value'];
	
	$row = mysql_fetch_array($result);
	$settings->resultsPageID = $row['value'];	
	
	$row = mysql_fetch_array($result);
	$settings->forumID = $row['value'];	
	
	$row = mysql_fetch_array($result);
	$settings->fullLockoutPenalty = $row['value'];
	
	$row = mysql_fetch_array($result);
	$settings->partialLockoutPenalty = $row['value'];
	
	return $settings;
}

function updateSubmittedTeamsPageID($submittedTeamsPageID, $leagueId) {
	$sql = "UPDATE settings set value = '$submittedTeamsPageID' where name = 'SUBMITTED_TEAMS_PAGE_ID' and leagueId = $leagueId;";
	mysql_query($sql);
}

function updateResultsPageID($resultsPageID, $leagueId) {
	$sql = "UPDATE settings set value = '$resultsPageID' where name = 'RESULTS_PAGE_ID' and leagueId = $leagueId;";
	mysql_query($sql);
}

function getUserSettings($coachName, $leagueId) {
	$sql = "SELECT * from settings where coachName = '$coachName' and leagueId = $leagueId order by id;";
	
	$result = mysql_query($sql);
	
	$settings = new UserSettings();
	$settings->coachName = $coachName;
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$setting = new UserSetting();
		$setting->name = $row['name'];
		$setting->value = $row['value'];
		
		$array[] = $setting;
	}
	$settings->settings = $array;
		
	
	return $settings;
}

function updateSystemSettings($trades, $timeLimit, $bot, $timeLength, $tradesPageID, $picksPageID, $seasonInProgress, $submittedTeamsPageID, $resultsPageID, $forumID, $fullLockoutPenalty, $partialLockoutPenalty, $leagueId) {
	$sql = "UPDATE settings set value = '$trades' where name = 'TRADES' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$timeLimit' where name = 'TIME_LIMIT' and leagueId = $leagueId;";
	mysql_query($sql);

	$sql = "UPDATE settings set value = '$bot' where name = 'BOT' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$timeLength' where name = 'TIME_LIMIT_LENGTH' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$tradesPageID' where name = 'TRADE_PAGE_ID' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$picksPageID' where name = 'PICKS_PAGE_ID' and leagueId = $leagueId;";
	mysql_query($sql);			
	
	$sql = "UPDATE settings set value = '$seasonInProgress' where name = 'SEASON_IN_PROGRESS' and leagueId = $leagueId;";
	mysql_query($sql);	

	$sql = "UPDATE settings set value = '$submittedTeamsPageID' where name = 'SUBMITTED_TEAMS_PAGE_ID' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$resultsPageID' where name = 'RESULTS_PAGE_ID' and leagueId = $leagueId;";
	mysql_query($sql);	
	
	$sql = "UPDATE settings set value = '$forumID' where name = 'FORUM_ID' and leagueId = $leagueId;";
	mysql_query($sql);	
	
	$sql = "UPDATE settings set value = '$fullLockoutPenalty' where name = 'FULL_LOCKOUT_PENALTY' and leagueId = $leagueId;";
	mysql_query($sql);
	
    $sql = "UPDATE settings set value = '$partialLockoutPenalty' where name = 'PARTIAL_LOCKOUT_PENALTY' and leagueId = $leagueId;";
	mysql_query($sql);
	
}

function updateUserSettings($coachName, $tradesPerPage, $reviewsPerPage, $usePreferences, $draftTab, $prefTab, $archiveTab, $statusTab, $overviewTab, $leagueId) {
	if ($reviewsPerPage != -1) {
		$sql = "UPDATE settings set value = '$reviewsPerPage' where name = 'KPPSTATUS_PER_PAGE' and coachName = '$coachName' and leagueId = $leagueId;";
		mysql_query($sql);
	}
	
	$sql = "UPDATE settings set value = '$tradesPerPage' where name = 'TRADES_PER_PAGE' and coachName = '$coachName' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$usePreferences' where name = 'USE_PREFERENCES' and coachName = '$coachName' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$draftTab' where name = 'SHOW_DRAFT_TAB' and coachName = '$coachName' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$prefTab' where name = 'SHOW_PREF_TAB' and coachName = '$coachName' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$archiveTab' where name = 'SHOW_ARCHIVE_TAB' and coachName = '$coachName' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$statusTab' where name = 'SHOW_REVIEW_TAB' and coachName = '$coachName' and leagueId = $leagueId;";
	mysql_query($sql);
	
	$sql = "UPDATE settings set value = '$overviewTab' where name = 'SHOW_OVERVIEW_TAB' and coachName = '$coachName' and leagueId = $leagueId;";
	mysql_query($sql);
	
}
?>